package com.example.mk.UI.Mine;

import android.content.Context;
import android.media.Image;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.mk.R;
import com.example.mk.UI.Mine.data.MineDataBase;
import com.example.mk.UI.Mine.data.MineDataHeader;
import com.example.mk.UI.Mine.data.MineDataItem;
import com.example.mk.UI.Mine.data.MineDataType;
import com.google.android.material.imageview.ShapeableImageView;

import java.util.ArrayList;

public class MineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;
    private ArrayList<MineDataBase> mDataList;
    private View.OnClickListener mListener;

    public MineAdapter(Context context, ArrayList<MineDataBase> dataList, View.OnClickListener listener) {
        mContext = context;
        mDataList = dataList;
        mListener = listener;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        LayoutInflater inflater = LayoutInflater.from(mContext);
        if (viewType == MineDataType.separator.ordinal()) {
            return new SeparatorHolder(inflater.inflate(R.layout.fragment_mine_separator, parent, false));
        } else if (viewType == MineDataType.item.ordinal()) {
            return new ItemHolder(inflater.inflate(R.layout.fragment_mine_item, parent, false));
        } else if (viewType == MineDataType.header.ordinal()) {
            return new HeaderHolder(inflater.inflate(R.layout.fragment_mine_header, parent, false));
        } else if (viewType == MineDataType.line.ordinal()) {
            return new LineHolder(inflater.inflate(R.layout.fragment_mine_line, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SeparatorHolder) {
            // do nothing
        } else if (holder instanceof ItemHolder) {
            ItemHolder viewHolder = (ItemHolder)holder;
            MineDataItem mineDataItem = (MineDataItem)mDataList.get(position);
            viewHolder.textView.setText(mineDataItem.getTitle());
            viewHolder.imageView.setImageResource(mineDataItem.getImgId());
        } else if (holder instanceof HeaderHolder) {
            HeaderHolder viewHolder = (HeaderHolder)holder;
            MineDataHeader mineDataHeader = (MineDataHeader)mDataList.get(position);
            viewHolder.nickTextView.setText(mineDataHeader.getNick());
            viewHolder.infoTextView.setText(mineDataHeader.getInfo());
        } else if (holder instanceof LineHolder) {
            // do nothing
        }
    }

    @Override
    public int getItemCount() {
        return mDataList.size();
    }

    @Override
    public int getItemViewType(int position) {
        MineDataBase mineDataBase = mDataList.get(position);
        return mineDataBase.type.ordinal();
    }

    public class ItemHolder extends RecyclerView.ViewHolder {
        private ImageView imageView;
        private TextView textView;

        public ItemHolder(View itemView) {
            super(itemView);

            imageView = itemView.findViewById(R.id.item_img);
            textView = itemView.findViewById(R.id.item_title);
            itemView.setOnClickListener(mListener);
        }
    }

    public class SeparatorHolder extends RecyclerView.ViewHolder {
        private View view;
        public SeparatorHolder(View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.item_separator);
        }
    }

    public class HeaderHolder extends RecyclerView.ViewHolder {
        private ShapeableImageView iconImgView;
        private TextView nickTextView;
        private TextView infoTextView;
        private ImageView sexImgView;
        private ImageView qrCodeImgView;

        public HeaderHolder(View itemView) {
            super(itemView);
            iconImgView = itemView.findViewById(R.id.item_header_icon);
            nickTextView = itemView.findViewById(R.id.item_header_nick);
            infoTextView = itemView.findViewById(R.id.item_header_info);
            sexImgView = itemView.findViewById(R.id.item_header_sex_img);
            qrCodeImgView = itemView.findViewById(R.id.item_header_qr_code_img);
        }
    }

    public class LineHolder extends RecyclerView.ViewHolder {
        private View view;
        public LineHolder(View itemView) {
            super(itemView);
            view = itemView.findViewById(R.id.item_line);
        }
    }
}
